/*
 * @lc app=leetcode.cn id=134 lang=cpp
 *
 * [134] 加油站
 */

// @lc code=start
class Solution
{
public:
    int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
    {
        const int n = gas.size();
        int i = 0;
        while (i < n)
        {
            int sum_of_gas = 0;
            int sum_of_cost = 0;
            int cnt = 0;
            while (cnt < n)
            {
                sum_of_gas += gas[(i + cnt) % n];
                sum_of_cost += cost[(i + cnt) % n];
                if (sum_of_gas < sum_of_cost)
                {
                    break;
                }

                cnt++;
            }
            if (cnt == n)
            {
                return i;
            }
            i = i + cnt + 1;
        }

        return -1;
    }
};
// @lc code=end
